package com.woniuxy.user.controller;

import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.UserPo;
import com.woniuxy.commons.entity.UserVo;
import com.woniuxy.user.service.UserLoginService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author:阳佳
 * @Date: 2020/8/5 9:38
 * @Description:
 * @PACKAGE_NAME:com.woniuxy.user.controller
 */
@Api(tags = "用户登录注册登录")
@RestController
public class UserLoginController {
    @Autowired
    private UserLoginService userLoginService;

    @GetMapping("/login")
    @ResponseBody

    public ResponseEntity<String> loginByAccount(UserVo userVo, HttpSession httpSession) {
        UserPo userPo = userLoginService.findUserPOByAccount(userVo.getAccount());
        if (userPo != null && userVo.getPassword().equals(userPo.getPassword())) {
            httpSession.setAttribute("user",userPo);
            return new ResponseEntity<String>(202, "登陆成功");
        }
        return new ResponseEntity<String>(201, "登陆失败");
    }

    @PostMapping("/register")
    @ResponseBody
    public ResponseEntity<String> insertUser(UserVo userVo) { // 前端传入的user

        // 先判断 手机号 账号是否已存在
        if (userLoginService.findUserPOByAccount(userVo.getAccount()) != null) {
            return new ResponseEntity<String>(11, "账号已存在");
        }
        // 判断两次密码是否一致
        if (!userVo.getPassword().equals(userVo.getRepassword())) {
            return new ResponseEntity<String>(1, "两次密码不一致");
        }

        // 创建空的UserPO对象
        UserPo userPO = new UserPo();
        // 将VO的值赋值给PO

        BeanUtils.copyProperties(userPO, userVo);

        userLoginService.insertUser(userPO);
        return new ResponseEntity<String>(200, "注册成功");
    }


}
